﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace BookStore
{
    public partial class DangKy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            MembershipUser newUser = Membership.GetUser(CreateUserWizard1.UserName);
            Guid newUserId = (Guid)newUser.ProviderUserKey;

            //Assign role to user
            using (var context = new BookStoreEntities())
            {
                aspnet_Roles khRole = (from r in context.aspnet_Roles
                                       where r.RoleName == "KhachHang"
                                       select r).First();
                aspnet_UsersInRoles userinrole = new aspnet_UsersInRoles();
                userinrole.RoleId = khRole.RoleId;
                userinrole.UserId = newUserId;
                context.AddToaspnet_UsersInRoles(userinrole);
                context.SaveChanges();
            }

            string FullName = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("FullName")).Text;
            string Address = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Address")).Text;
            string PhoneNumber = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("PhoneNumber")).Text;
            string Email = ((TextBox)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Email")).Text;

            using (var context = new BookStoreEntities())
            {
                KhachHang newkh = new KhachHang();
                newkh.TenKhachHang = FullName;
                newkh.DiaChi = Address;
                newkh.DienThoai = PhoneNumber;
                newkh.Email = Email;
                newkh.UserID = newUserId;

                context.KhachHangs.AddObject(newkh);
                context.SaveChanges();
            }
        }
    }
}